package com.rdzn.mall.portal.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rdzn.mall.base.common.Result;
import com.rdzn.mall.center.entity.PmsProduct;
import com.rdzn.mall.center.entity.UmsMember;
import com.rdzn.mall.center.service.IPmsProductHotService;
import com.rdzn.mall.center.service.IPmsProductService;
import com.rdzn.mall.center.service.IUmsFootprintService;
import com.rdzn.mall.portal.domain.PmsPortalProductDetail;
import com.rdzn.mall.portal.service.IProductService;
import com.rdzn.mall.portal.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
* @Description: pms_product
* @Author: jeecg-boot
* @Date:   2020-08-27
* @Version: V1.0
*/
@Api(tags="pms_product")
@RestController
@RequestMapping("/goods")
@Slf4j
public class MPmsProductController extends JeecgController<PmsProduct, IPmsProductService> {
    @Autowired
    private IProductService productService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IPmsProductHotService pmsProductHotService;
    @Autowired
    private IUmsFootprintService umsFootprintService;

   /**
    * 分页列表查询
    *
    * @param pmsProduct
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "pms_product-分页列表查询")
   @ApiOperation(value="pms_product-分页列表查询", notes="pms_product-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(PmsProduct pmsProduct,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       QueryWrapper<PmsProduct> queryWrapper = QueryGenerator.initQueryWrapper(pmsProduct, req.getParameterMap());
       return Result.ok(productService.pageList(pmsProduct, pageNo, pageSize, queryWrapper));
   }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "pms_product-通过id查询")
    @ApiOperation(value="pms_product-通过id查询", notes="pms_product-通过id查询")
    @GetMapping(value = "/detail")
    public Result<?> queryById(@RequestParam(name="id",required=true) Integer id) {

        PmsPortalProductDetail detail = productService.getDetail(id);
        if(detail == null) {
            return Result.error("未找到对应数据");
        }
        //添加热度更新
        pmsProductHotService.refresh(id);
        //添加足迹
        UmsMember umsMember = userService.getCurrentMember();
        if (umsMember != null){
            umsFootprintService.addFoot(umsMember.getId(), id);
        }
        return Result.ok(detail);
    }


}
